package com.example.fourm.controller;

import com.example.fourm.common.AppResult;
import com.example.fourm.exception.ApplicationException;
import com.example.fourm.model.Board;
import com.example.fourm.services.IBoardService;
import jakarta.annotation.Resource;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RequestMapping("/board")
@RestController
public class BoardController {

    @Value("${forum.index.board-num:5}")
    private Integer indexBoardNum;
    @Resource
    private IBoardService boardService;

    @GetMapping("/topList")
    public AppResult<List<Board>> topList(){
        log.info("topList : " + indexBoardNum);
        List<Board> boards = boardService.selectByNum(indexBoardNum);
        if(boards == null){
            boards = new ArrayList<Board>();
        }
        return AppResult.success(boards);
    }
    @GetMapping("/getById")
    public AppResult<Board> getById(@RequestParam("id")@NonNull Long id){
        Board board = boardService.selectByPrimaryKey(id);
        if(board == null || board.getDeleteState()==1){
            log.warn("板块不存在");
            throw new ApplicationException("板块不存在");
        }
        return AppResult.success(board);
    }
}
